package com.youmu.sichuansfc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
import org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration;
import org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.WebApplicationInitializer;
/**
 * 应用启动的主入口
 */
@Configuration
// 此三项也未用到，但二次开发时有可能会用到，暂时不屏蔽：JdbcTemplateAutoConfiguration.class, JtaAutoConfiguration.class,
// WebSocketAutoConfiguration.class
// 暂时不用Mobile的自动配置：DeviceResolverAutoConfiguration.class, SitePreferenceAutoConfiguration.class
@EnableAutoConfiguration(exclude = { MessageSourceAutoConfiguration.class, JmxAutoConfiguration.class,
		CacheAutoConfiguration.class, DeviceResolverAutoConfiguration.class, SitePreferenceAutoConfiguration.class})
//@PropertySource({ "classpath:conf/spring.jpa.properties", "${weixin.config.file}" })
//@ImportResource({ "classpath:conf/**/context*.xml", "classpath:custom.xml" })
// 创建war需要继承SpringBootServletInitializer
// Weblogic要求直接实现WebApplicationInitializer接口，即使SpringBootServletInitializer已经实现了这个接口
// {"com.foo.dal.*","com.foo.notification.*"}
//@PropertySource({ "classpath:META-INF/*.xml"})
@ComponentScan(value={"com.youmu.sichuansfc.*"})
//@SpringBootApplication
//@EnableTransactionManagement
@EntityScan("com.youmu.sichuansfc.domain") // 扫描实体类 jpa包
//exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class},
@SpringBootApplication(scanBasePackages = {"com.youmu.sichuansfc"})
//@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
//@ComponentScan(basePackages = { "com.apress.prospringmvc.bookstore.web" })
public class Application extends SpringBootServletInitializer  implements WebApplicationInitializer {
	/**
	 * war方式启动的处理方法
	 */
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return configureApplication(builder);
	}

	/**
	 * jar方式启动的处理方法
	 * 
	 * @param args
	 * @throws Exception
	 */

	public static void main(String[] args)  throws Exception {
		//configureApplication(new SpringApplicationBuilder()).run(args);
		SpringApplication.run(Application.class, args);
	}

	/**
	 * war方式启动和jar方式启动共用的配置
	 * 
	 * @param builder
	 * @return
	 */
	private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) {
		return builder.sources(Application.class).listeners(
				new ApplicationListener<ApplicationEnvironmentPreparedEvent>() {
					// 在应用环境准备好后执行（Application.properties和PoropertySource已读取），此时BeanFactory还未准备好（Bean还未创建）
					public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
						ConfigurableEnvironment env = event.getEnvironment();
						// 用配置文件中的内容覆盖替代Constants的内容
						//Constants.loadEnvironment(env);
					}
				});
	}

}